package org.funtester.performance.books.chapter06.section3;

import org.funtester.performance.books.chapter03.section3.ThreadTask;
import org.funtester.performance.books.chapter03.section4.TaskExecutor;
import org.funtester.performance.books.chapter04.section3.RandomTool;
import org.funtester.performance.books.chapter06.section2.MySqlBase;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 新用户注册insert数据库
 */
public class UserRegisterCase {

    public static void main(String[] args) throws InterruptedException {
        String url = "jdbc:mysql://127.0.0.1:3306/funtester";// 服务端地址
        String user = "root";// 用户名
        String password = "funtester";// 密码
        int totalNum = 1000;// 总执行次数
        int threadNum = 8;// 线程数
        int rumpUpTime = 20;// Rump-Up运行时间
        List<ThreadTask> tasks = new ArrayList<>();// 任务集合
        String sqlFormat = "insert into user (name, age, level, region, address) values ('%s', %d, %d, '%s', '%s')";
        AtomicInteger successUser = new AtomicInteger();// 成功次数
        for (int i = 0; i < threadNum; i++) {
            ThreadTask threadTask = new ThreadTask() {

                /**
                 * 数据库操作基础类
                 */
               MySqlBase base;

                @Override
                public void before() {
                    super.before();// 调用父类的方法
                    base = new MySqlBase(url, user, password);// 创建数据库操作基础类
                    try {
                        base.init();// 初始化资源
                    } catch (SQLException e) {
                        throw new RuntimeException(e);// 抛出运行时异常
                    }
                }

                @Override
                public void test() {
                    String name = RandomTool.getRandomString(32);// 随机生成姓名
                    int age = ThreadLocalRandom.current().nextInt(100);// 随机生成年龄
                    int level = ThreadLocalRandom.current().nextInt(10);// 随机生成等级
                    String region = RandomTool.getRandomString(32);// 随机生成地区
                    String address = RandomTool.getRandomString(512);// 随机生成地址
                    String sql = String.format(sqlFormat, name, age, level, region, address);// 格式化 SQL 语句
                    try {
                        int i = base.statement.executeUpdate(sql);// 执行插入语句
                        if (i == 1) successUser.getAndIncrement();// 成功注册用户数量加一
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }

                @Override
                public void after() {
                    super.after();// 调用父类的方法
                    try {
                        base.close();// 关闭资源
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
            threadTask.totalNum = totalNum;// 设置执行次数
            threadTask.costTime = new ArrayList<>(totalNum);// 设置任务的执行时间集合,设置容量,避免频繁扩容
            tasks.add(threadTask);// 添加任务
        }
        TaskExecutor taskExecutor = new TaskExecutor(tasks, "新用户注册insert数据库", rumpUpTime);
        taskExecutor.start();// 启动任务执行器
        System.out.println("成功注册用户数量: " + successUser.get());
    }

}
